<template>
  <div class="modify-dialog">
    <slot>
      <el-button size="small" type="primary" @click="showDialog()">
        新增调货单
      </el-button>
    </slot>
    <el-dialog v-bind="dialogOpts" :visible.sync="dialogVisible">
      <Form v-bind="formOpts" v-on="formEvent">
        <template #formSuf v-if="modify == 'add'">
          <el-form-item label="材料" style="width: 100%">
            <ProductInput v-model="arr" />
          </el-form-item>
        </template>
        <template #formBtn>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="onConfirm"> 确 定</el-button>
          </div>
        </template>
      </Form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ProductInput from "./ProductInput.vue";
import modifyMixins from "@/mixins/modifyMixins.vue";

export default {
  mixins: [modifyMixins],
  components: {
    ProductInput,
  },
  data() {
    return {
      dialogWidth: "600px",
      form: {
        orderName: {
          tag: "el-input",
          label: "订单名称",
          value: "",
          props: {
            clearable: true,
            filterable: true,
            placeholder: "请输入订单名称",
          },
        },
        outWarehouseId: {
          tag: "el-select",
          label: "调出仓库",
          value: "",
          items: [],
          props: {
            clearable: true,
            filterable: true,
          },
        },
        inWarehouseId: {
          tag: "el-select",
          label: "调入仓库",
          value: "",
          items: [],
          props: {
            clearable: true,
            filterable: true,
          },
        },
        remark: {
          tag: "el-input",
          label: "备注",
          value: "",
          props: {
            rows: 3,
            type: "textarea",
            clearable: true,
            placeholder: "请输入备注",
          },
        },
        arrivalVouchers: {
          tag: "Uploader",
          label: "到货凭证",
          value: [],
          props: {},
        },
      },
    };
  },
  computed: {},
  watch: {
    "$store.state.warehouseList": function () {
      this.setItems();
    },
    "form.inWarehouseId.value": function () {
      this.setItems();
    },
    "form.outWarehouseId.value": function () {
      this.setItems();
    },
  },
  mounted() {
  },
  methods: {
    setItems() {
      const { warehouseListItems: items } = this.$store.getters;
      const outValue = this.form.outWarehouseId.value;
      const inValue = this.form.inWarehouseId.value;
      this.form.outWarehouseId.items = items.filter((e) => e.value != inValue);
      this.form.inWarehouseId.items = items.filter((e) => e.value != outValue);
      this.formKey = this.$quinn.guid();
    },
    showDialog(modify = "add", row = {}) {
      this.dialogVisible = true;
      this.row = row;
      this.modify = modify || "add";
      this.setItems();
      let keys = ["orderName", "outWarehouseId", "inWarehouseId", "remark"];
      if (modify == "upload") keys = ["arrivalVouchers"];
      for (const key in this.form) {
        let defValue = "";
        if (Array.isArray(this.form[key].value)) defValue = [];
        if (Object.hasOwnProperty.call(row, key))
          this.form[key].value = row[key];
        else this.form[key].value = defValue;
        this.form[key].hidden = true;
        if (keys.includes(key)) this.form[key].hidden = false;
      }
      this.formKey = this.$quinn.guid();
    },
    onSubmit(form) {
      const { modify, row } = this;
      if (modify === "add") {
        form.materialList = this.arrValues;
        this.$api["materialTransferMaterialTransferAdd"](form).then(
          this.submitDoneMsg
        );
      } else if (modify == "upload") {
        form.arrivalVouchers = form.arrivalVouchers.join(",");
        this.$api
          .materialTransferMaterialTransferUpdate({
            ...form,
            transferId: row.transferId,
          })
          .then(this.submitDoneMsg);
      }
    },
  },
};
</script>

<style lang="less" scoped></style>
